package Server.tool;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BeanToJson {
    public static String toJsonArrayString(Object tobean){
        JSONArray jsonArray = JSONArray.fromObject(tobean);
        return jsonArray.toString();
    }
    public static String toJsonObjectString(Object tobean){
        JSONObject jsonObject = JSONObject .fromObject(tobean);
        return jsonObject.toString();
    }
    public static Object jsonTobean(String json,Class toClass){
        com.alibaba.fastjson.JSONObject jsonObject= com.alibaba.fastjson.JSONObject.parseObject(json);
        return  com.alibaba.fastjson.JSON.parseObject(jsonObject.toJSONString(), toClass);
    }
    public static String bspcomm(JSONObject jsonObject){
        StringBuilder where = new StringBuilder();
        List<Map<String, String>> whereMAP = (List<Map<String, String>>) jsonObject.getJSONArray("common");
       /* String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
        Pattern p = Pattern.compile(regEx);*/

        //ZDNAME,ZDVAL,LIKE
        for (int i=0;i<whereMAP.size();i++) {
            Map<String, String> ZD = whereMAP.get(i);
            /*Matcher m = p.matcher(ZD.get("ZDNAME"));//这里把想要替换的字符串传进来
            Matcher m1 = p.matcher(ZD.get("ZDVAL"));*/
            /*ZD.put("ZDNAME",m.replaceAll("").trim());
            ZD.put("ZDVAL",m1 .replaceAll("").trim());*/
            if(ZD.get("ZDNAME").length()>=25||ZD.get("ZDVAL").length()>=200){
                return null;
            }
            if(ZD.get("ZDNAME").equals("1")/*||ZD.get("ZDVAL").indexOf(ZD.get("ZDNAME"))>=0*/){
                return null;
            }else{
                if (ZD.get("LIKE").equals("1")) {
                    where.append(" AND " + ZD.get("ZDNAME") + " = '" + ZD.get("ZDVAL") + "' ");
                } else if(ZD.get("LIKE").equals("0")){
                    where.append(" AND " + ZD.get("ZDNAME") + " like '%" + ZD.get("ZDVAL") + "%' ");
                }else if(ZD.get("LIKE").equals("2")) {
                    where.append(" AND " + ZD.get("ZDNAME") + " <> '" + ZD.get("ZDVAL") + "' ");
                }else if(ZD.get("LIKE").equals("3")) {
                    where.append(" or " + ZD.get("ZDNAME") + " = '" + ZD.get("ZDVAL") + "' ");
                }else if(ZD.get("LIKE").equals("4")) {
                    where.append(" or " + ZD.get("ZDNAME") + " <> '" + ZD.get("ZDVAL") + "' ");
                }else if(ZD.get("LIKE").equals("5")) {
                    where.append(" AND " + ZD.get("ZDNAME") + " <= '" + ZD.get("ZDVAL") + "' ");
                }else if(ZD.get("LIKE").equals("6")) {
                    where.append(" AND " + ZD.get("ZDNAME") + " >= '" + ZD.get("ZDVAL") + "' ");
                }else if(ZD.get("LIKE").equals("7")) {
                    where.append(" AND DATE(" + ZD.get("ZDNAME") + ") >= STR_TO_DATE('" + ZD.get("ZDVAL") + "','%Y-%m-%d')");
                }else if(ZD.get("LIKE").equals("8")) {
                    where.append(" AND DATE(" + ZD.get("ZDNAME") + ") <= STR_TO_DATE('" + ZD.get("ZDVAL") + "','%Y-%m-%d')");
                }else if(ZD.get("LIKE").equals("9")) {
                    where.append(" AND "+ZD.get("ZDNAME")+" in "+ZD.get("ZDVAL")+"");


                }
            }
            /*if(i==0){
                String opx = where.toString().replace("AND","");
                opx=opx.replace("or","");
                where.setLength(0);
                where.append(opx);
            }*/

        }
        return where.toString();
    }
}
